package com.by.hellosecurityoauth2.controller;

import com.by.hellosecurityoauth2.controller.request.RoleRequestBean;
import com.by.hellosecurityoauth2.controller.request.UserRoleRequestBean;
import com.by.hellosecurityoauth2.domain.TRole;
import com.by.hellosecurityoauth2.domain.TUser;
import com.by.hellosecurityoauth2.domain.TUserRole;
import com.by.hellosecurityoauth2.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 白羽
 * @Date 2022-12-03 21:26:01
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
public class UserController {
    /**
     * 服务对象
     */
    private UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @Operation(summary = "获取用户分页列表")
    @PreAuthorize("hasAuthority('user:user:get')")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public Page<TUser> list(@RequestParam @Parameter(description = "页数从1开始", required = true) int page,
                            @RequestParam @Parameter(description = "每页的条数", required = true) int limit,
                            HttpServletRequest request) {

        return userService.list(page - 1, limit);

    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @Operation(summary = "根据id获取用户信息")
    @PreAuthorize("hasAuthority('user:user:get')")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public TUser getUserById(@PathVariable("id") Long id) {
        return userService.findById(id);
    }

    @Operation(summary = "创建用户信息")
    @PreAuthorize("hasAuthority('user:user:create')")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public TUser createUser(@RequestBody RoleRequestBean bean, HttpServletRequest request) {
        return userService.create(bean);
    }

    @Operation(summary = "根据id修改用户信息")
    @PreAuthorize("hasAuthority('user:user:update')")
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public TUser updateUserById(@PathVariable("id") Long id, @RequestBody RoleRequestBean bean, HttpServletRequest request) {
        return userService.updateById(id, bean);
    }

    @Operation(summary = "根据id删除用户信息")
    @PreAuthorize("hasAuthority('user:user:delete')")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public void deleteUserById(@PathVariable("id") Long id, HttpServletRequest request) {
        userService.deleteById(id);
    }

    @Operation(summary = "根据用户id批量配置用户角色信息")
    @PreAuthorize("hasAuthority('user:user:update')")
    @RequestMapping(value = "/role/{id}", method = RequestMethod.PUT)
    public List<TUserRole> updateUserRole(@PathVariable("id") Long id, @RequestBody List<UserRoleRequestBean> bean, HttpServletRequest request) {
        return userService.updateUserRole(id, bean);
    }

    @Operation(summary = "根据用户id获取用户角色信息")
    @PreAuthorize("hasAuthority('user:user:get')")
    @RequestMapping(value = "/role/{id}", method = RequestMethod.GET)
    public List<TUserRole> getUserRoleByUserId(@PathVariable("id") Long id, HttpServletRequest request) {
        return userService.getUserRoleByUserId(id);
    }



}